package utils;

import android.content.Context;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;


public class OtherUtil {
    public static String verStr = "1.0.1.20250421";

    public static Boolean submitEnable = false;

    private static Boolean faceCapture = false; //是否捕捉到活体 等待确认发送验证
    private static Boolean faceEnable = false;

    private static Context ctx ;
    private static OtherUtil singletonOtherUtil = new OtherUtil();
    //将构造器设置为private禁止通过new进行实例化
    private OtherUtil() {

    }
    public static OtherUtil getInstance() {
        return singletonOtherUtil;
    }


    public static Boolean setMainContext(Context argctx){
        ctx = argctx;
        return true;
    }
    public static Context getMainContext(){
        return ctx;
    }
    public static Boolean isFaceCapture(){
        return faceCapture;
    }
    public static Boolean FaceCaptureEnable(){
        faceCapture = true;
        return true;
    }
    public static Boolean FaceCaptureDisable(){
        faceCapture = false;
        return true;
    }

    public static Boolean isFaceEnable(){
        return faceEnable;
    }
    public static Boolean FaceEnableEnable(){
        faceEnable = true;
        return true;
    }
    public static Boolean FaceEnableDisable(){
        faceEnable = false;
        return true;
    }

    public static Boolean isSubmitEnable(){
        return submitEnable;
    }
    public static Boolean SubmitEnable(){
        submitEnable = true;
        return true;
    }
    public static Boolean SubmitDisable(){
        submitEnable = false;
        return true;
    }
    public static boolean isKeyboardActive(Window win){

        if(win.getAttributes().softInputMode == WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE){
            //Toast.makeText(FullscreenActivity.this, "显示", Toast.LENGTH_SHORT).show();
            return true;
        }else {
            //Toast.makeText(FullscreenActivity.this, "没显示", Toast.LENGTH_SHORT).show();
            return false;

        }

    }
    private boolean isExistUID(Context ctx) {
        try {
            String uid = getUID(ctx);
            if(uid.length() == 0){
                return false;
            }
            return true;

        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    private String createUID(Context ctx){
        String uid= String.valueOf(System.currentTimeMillis());
        setUID(ctx,uid);
        return uid;
    }
    private String getUID(Context ctx){
        try {
            StringBuilder sb = new StringBuilder("");
            FileInputStream inStream = ctx.openFileInput("uid.txt");
            byte[] buffer = new byte[1024];
            int len = inStream.read(buffer);
            //读取文件内容
            while(len > 0){
                sb.append(new String(buffer,0,len));

                //继续将数据放到buffer中
                len = inStream.read(buffer);
            }
            //关闭输入流
            inStream.close();
            return sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }
    private boolean setUID(Context ctx, String uuid){
        try {
            FileOutputStream outStream = ctx.openFileOutput("uid.txt", Context.MODE_PRIVATE);
            outStream.write(uuid.getBytes());
            outStream.close();
            return true;

        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static String getCurrentTime(){
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(d);
    }
    public String getAppVersion() {

        return verStr;
    }
    public String getAppSerial(Context ctx) {

        if(isExistUID(ctx) == true){
            return getUID(ctx);
        }else{
            createUID(ctx);
            return getUID(ctx);
        }
    }

}
